/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.PurchasedProductBeanLocal;
import ejb.TiendaliaOrderBeanLocal;
import entities.Product;
import entities.PurchasedProduct;
import entities.TiendaliaOrder;
import entities.User;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Jesus
 */
@ManagedBean
@SessionScoped
@Stateful
public class CartBean implements Serializable {

    @EJB
    private TiendaliaOrderBeanLocal orderBean;
    @EJB
    private PurchasedProductBeanLocal purchasedProductBean;
    public List<PurchasedProduct> cartProducts;
    private TiendaliaOrder order;
    private PurchasedProduct selectedProduct;

    public CartBean() {
        cartProducts = new ArrayList<PurchasedProduct>();
    }

    @PostConstruct
    public void init() {
    }

    public List<PurchasedProduct> getCartProducts() {
        return cartProducts;
    }

    public void setCartProducts(List<PurchasedProduct> cartProducts) {
        this.cartProducts = cartProducts;
    }

    public TiendaliaOrder getOrder() {
        return order;
    }

    public void setOrder(TiendaliaOrder order) {
        this.order = order;
    }

    public PurchasedProduct getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(PurchasedProduct selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public void addProduct(Product product, Integer quantity) {
        System.out.println(product + ": " + quantity);
        PurchasedProduct purchasedProduct = new PurchasedProduct(product, quantity);
        cartProducts.add(purchasedProduct);
    }

    public String confirmOrder() {
        System.out.println("CONFIRM");
        order = new TiendaliaOrder();
        order.setCustomer(getUserFromSession());
        for (PurchasedProduct product : cartProducts) {
            product.setPurchase(order);
        }
        order.setOrderProducts(cartProducts);

        order = orderBean.insert(order);
        cartProducts.clear();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El pedido " + order.getId() + " se ha realizado con éxito", ""));

        return "main";
    }

    private User getUserFromSession() {
        User customer = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
        return customer;
    }

    public Double getPurchaseTotal() {
        Double acum = 0d;
        for (PurchasedProduct p : cartProducts) {
            acum += p.getTotal();
        }
        return acum;
    }

    public void removeProduct() {
        cartProducts.remove(selectedProduct);
    }
}
